
package cn.edu.shengda;

import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Color;
        
public class TestFrame extends JFrame implements ActionListener {
    TestFrame() {
        init();
        setLayout(null);
        this.setResizable(false);
        this.setTitle("JavaApplication");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setBounds(400,100,800,600);
        this.setVisible(true);
    }
    public void init() {

        Font font = new Font("微软雅黑",Font.BOLD, 18);
     ;
        radioButtonRectangle = new JRadioButton("矩形");
        radioButtonRectangle.setFont(font);
        radioButtonRectangle.setSelected(true);
        radioButtonRectangle.setBounds(50, 45, 65, 25);
        widthlabel = new JLabel("W：");
        widthlabel.setFont(font);
        widthlabel.setBounds(150, 20, 40, 25);
        widthText = new JTextField("");
        widthText.setFont(font);
        widthText.setBounds(210, 20, 200, 25);
        heightlabel = new JLabel("H：");
        heightlabel.setFont(font);
        heightlabel.setBounds(150, 70, 40, 25);
        heightText = new JTextField("");
        heightText.setFont(font);
        heightText.setBounds(210, 70, 200, 25);
        radioButtonCircle = new JRadioButton("圆");
        radioButtonCircle.setBounds(50, 150, 45, 25);
        radioButtonCircle.setFont(font);
        radiuslabel = new JLabel("r：");
        radiuslabel.setFont(font);
        radiuslabel.setBounds(150, 150, 60, 25);
        radiusText = new JTextField("");
        radiusText.setFont(font);
        radiusText.setBounds(210, 150, 200, 25);
        calJButton = new JButton("确认");
        calJButton.setFont(font);
        calJButton.setBounds(550, 60, 80, 60);

        label = new JLabel("结果：");
        label.setFont(font);
        label.setBounds(50, 200, 60, 25);

        widthInfo = new JLabel();
        widthInfo.setFont(font);
        widthInfo.setBounds(150, 260, 600, 25);
        heightInfo = new JLabel();
        heightInfo.setFont(font);
        heightInfo.setBounds(150, 300, 600, 25);
        radiusInfo = new JLabel();
        radiusInfo.setFont(font);
        radiusInfo.setBounds(150, 300, 600, 25);
        perimeterInfo = new JLabel();
        perimeterInfo.setFont(font);
        perimeterInfo.setBounds(150, 340, 600, 25);
        areaInfo = new JLabel();
        areaInfo.setFont(font);
        areaInfo.setBounds(150, 380, 600, 25);
        calJButton.addActionListener(this);
        add(radioButtonRectangle);
        add(widthlabel);
        add(widthText);
        add(heightlabel);
        add(heightText);
        add(radioButtonCircle);
        add(radiuslabel);
        add(radiusText);
        add(calJButton);
        add(label);
        add(widthInfo);
        add(heightInfo);
        add(radiusInfo);
        add(perimeterInfo);
        add(areaInfo);
        group = new ButtonGroup();
        group.add(radioButtonRectangle);
        group.add(radioButtonCircle);

    }
    JRadioButton radioButtonRectangle;
    JRadioButton radioButtonCircle;
    JTextField widthText;
    JTextField heightText;
    JTextField radiusText;
    JLabel widthlabel;
    JLabel heightlabel;
    JLabel radiuslabel;
    JLabel widthInfo;
    JLabel heightInfo;
    JLabel radiusInfo;
    JLabel perimeterInfo;
    JLabel areaInfo;
    JButton calJButton;
    JLabel label;
    ButtonGroup group;
    ShapeArea shapeArea;
    public void actionPerformed(ActionEvent e) {
        if (e.getSource().equals(calJButton)) {
            if (radioButtonRectangle.isSelected()) {
                radiusInfo.setText("");
                double width = Double.parseDouble(widthText.getText().trim());
                double height = Double.parseDouble(heightText.getText().trim());
                shapeArea = new Rectangle(width, height);
                widthInfo.setText("矩形的宽：" + String.format("%.2f", width));
                heightInfo.setText("矩形的高：" + String.format("%.2f", height));
                perimeterInfo.setText("矩形的周长：" + String.format("%.2f", shapeArea.getPerimeter()));
                areaInfo.setText("矩形的面积：" + String.format("%.2f", shapeArea.getArea()));
            }
            if (radioButtonCircle.isSelected()) {
                widthInfo.setText("");
                heightInfo.setText("");

                double radius = Double.parseDouble(radiusText.getText().trim());
                shapeArea = new Circle(radius);
                radiusInfo.setText("圆的半径：" + String.format("%.2f", radius));
                perimeterInfo.setText("圆的周长：" + String.format("%.2f", shapeArea.getPerimeter()));
                areaInfo.setText("圆的面积：" + String.format("%.2f", shapeArea.getArea()));
            }
        }
    }
    public static void main(String[] args) {
        new TestFrame();
    }
}
